<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Base\Charge;
use App\Models\Base\ServerList;
use App\Models\Game\PlayerGamedata;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Support\Facades\Input;

/**
 * 玩家等级.
 */
class LevelController extends Controller
{
    protected $server_id;

    public function __construct()
    {
        if (Input::get('serverId')) {
            $this->server_id = Input::get('serverId');
        } else {
            $all = ServerList::all();
            $this->server_id = $all[0]->server_id;
        }
        setDB($this->server_id);
    }

    /**
     * [dist 等级分布].
     *
     * @Author   王文凡
     * @DateTime 2018-05-02
     *
     * @version  1.0
     *
     * @return Content
     */
    public function dist()
    {
        return Admin::content(function (Content $content) {
            $content->header(trans('game.data_statistic'));
            $content->description(trans('game.grade_distribution'));
            $content->body($this->grid());
        });
    }

    /**
     * @return Grid
     */
    protected function grid()
    {
        $server = ServerList::where('server_id', $this->server_id)->first();
        $server_name = $server->name;

        return Admin::grid(PlayerGamedata::class, function (Grid $grid) use ($server_name) {
            $grid->model()->selectRaw('level,count(level) as number')->groupBy('level');
            $grid->column(trans('game.server_name'))->display(function () use ($server_name) {
                return $server_name;
            });
            $grid->level(trans('game.level'))->sortable();
            $grid->number(trans('game.number'))->sortable();
            $grid->disableActions();
            $grid->disableCreateButton();
            $grid->tools(function ($tools) {
                $tools->batch(function ($batch) {
                    $batch->disableDelete();
                });
            });
            $grid->filter(function ($filter) {
                // 去掉默认的id过滤器
                $filter->disableIdFilter();
                $filter->equal('serverId', trans('game.server_name'))->select(ServerList::all()->pluck('name', 'server_id'));
                $filter->equal('platformID', trans('game.channel'))->select(Charge::where('pid', 0)->pluck('name', 'channel_no'));
                $filter->setNotWhere(['serverId']);
            });
        });
    }
}
